package com.example.dee.filter;

import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * @ClassName CharsetFilter
 * @Description 用于过滤请求使得编码统一
 * @Author 李劲松
 * @Date 2022/7/19 0020 21:46
 * @Version V1.0
 **/
@Order(1)
@WebFilter(filterName = "charsetFilter",urlPatterns = "/*")
public class CharsetFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");//客户端->服务端
        servletResponse.setContentType("text/html;charset=utf-8");//服务端->客户端
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
